//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    detail: {
      title: "获取中...",
      content: "获取中..."
    },
    index: null,
    like_num:0
  },

  onLoad: function (option) {
	let that = this
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];  //上一个页面
    that.setData({
      detail: prevPage.data.letterList[parseInt(option.index)],
      index: parseInt(option.index)
    })
	if(prevPage.data.letterList[parseInt(option.index)].mail_show==2){
	
		app.httpGet('mail/getLikeNum',{
			letter_id:prevPage.data.letterList[parseInt(option.index)].id,
			user_id:app.globalData.dbInfo.id
		},function(res){
			if(res.status==1){
				that.setData({
					like_num:res.like_num
				})
			}
		})
		
	}

  },
  mailShow() {
    let that = this
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];  //上一个页面
    wx.showModal({
      title: "确定公开吗?该操作无法撤回",
      success(option) {
        if (option.confirm) {
          app.httpGet('Mail/setLetterPublic', {
            user_id: app.globalData.dbInfo.id,
            letter_id: that.data.detail.id
          }, function (res) {
            if (res.status == 1) {
              wx.showToast({
                title: res.msg
              })
              var detail = that.data.detail
              detail.content = res.content
              detail.mail_show = 1
              var allList = prevPage.data.letterList
              allList[that.data.index] = detail
              that.setData({
                detail: detail
              })
              prevPage.setData({
                letterList: allList
              })

            } else if (res.status == 0) {
              wx.showToast({
                title: res.msg,
                icon: "none"
              })
            } else {
              wx.showToast({
                title: "服务器异常，请联系作者！",
                icon: "none"
              })
            }
          })
        } else {
          console.log('取消')
        }
      }
    })
  },


  onShareAppMessage: function () {
    let that = this
    qq.showShareMenu({
      showShareItems: ['qq', 'qzone', 'wechatFriends', 'wechatMoment']
    })

    var title = "快来康康我写给未来的信——" + that.data.detail.title;
    console.log(title)

    return {
        title:title,
        desc: '写给未来的信',
        imageUrl: '/images/logo.png',
        path: '/pages/public/private_share/private_share?user_id=' + app.globalData.dbInfo.id + "&letter_id=" + that.data.detail.id
    }


  }

})
